/**
 * Created by Administrator on 2017/3/30.
 */
app.controller('detailActCtr',
    ['$scope', '$location', '$http', '$rootScope', '$routeParams', '$sce', function ($scope, $location, $http, $rootScope, $routeParams, $sce) {
        //loading提示
        $("#globalLoading").css({
            top:'10rem',
            opacity:'1'
        });
        //获取视活动详情数据
        var articleId = $routeParams.fileId;

        console.log("articleId");
        console.log(articleId);

        // articleId=测试155

        $http.jsonp(DetailActData+'siteID=1&fileId='+articleId+'&jsoncallback=JSON_CALLBACK'
        ).success(function (data, header, config, status) {
            $scope.actDetailData = data;
            console.log("活动详情");
            //是否关闭当前稿件评论功能
            $scope.currentDiscussClosed=data.discussClosed;

            console.log($scope.actDetailData);
            var datas=data.content;//获取动态模板
            var indexA=datas.indexOf("<body>");
            var indexB=datas.indexOf("<\/body>");
            var detailDatass=datas.substring((indexA+6) ,indexB);
            //后去除去body标签内容
            $scope.conAct=detailDatass;

            // 非组图图片
            var i=0;
            var reg=/<!--IMAGEARRAY#[0-9]{1,}-->/gi;
            $scope.conAct=$scope.conAct.replace(reg,function () {
                return '<img src="'+data.images[0].imagearray[i++].imageUrl+'">';
            });

            $scope.trustHtmlAct = $sce.trustAsHtml($scope.conAct);
            if(indexA==-1){
                $scope.trustHtmlAct = $sce.trustAsHtml(datas);
            }

            //loading提示
            $("#globalLoading").animate({
                top:'20px',
                opacity:'0'
            },500);
        }).error(function (data, header, config, status) {
        });


        /**
         * 活动ource=6 活动视频直播完全相同部分
         * */


        //获取最新评论id=3969
        $scope.getNewCom=function () {
            $scope.commeInputs=siteConfig.defaultHint;
            // console.log(compage);
            $http.jsonp(newComment+'id='+articleId+'&siteId=1&lastFileId=0&page=0&source='+actSource+'&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("新评论");
                console.log(data);
                // 是否还有更多评论
                $scope.hasMorCom=data.hasMore;
                $scope.newCommenCon=data.list;
                console.log($scope.newCommenCon);
                $scope.newComCounts=data.totalCount;
                console.log($scope.newComCounts);
            }).error(function(data,header,config,status){

            });
        };


        //获取 热门评论'+articleId+' 测试3969
        $scope.getHotCom=function () {
            $http.jsonp(hotComment+'id='+articleId+'&siteId=1&source='+actSource+'&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("热门评论")
                console.log(data)
                $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        };


        //获取全局配置信息
        $scope.getCommentInfo=function(){
            $http.jsonp(commInfo+'&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("全局评论信息");
                console.log(data);
                $scope.siteConfig=data.siteConfig.discuss;
                //评论框提示语
                siteConfig.defaultHint=$scope.siteConfig.defaultHint;
                //官方评论用户名
                siteConfig.defaultName=$scope.siteConfig.defaultName;
                $scope.defaultName=$scope.siteConfig.defaultName;
                //官方评论头像
                siteConfig.defaultIcon=$scope.siteConfig.defaultIcon;
                $scope.siteConfigDefaultIcon=$scope.siteConfig.defaultIcon;
                console.log("官方用户名");
                console.log($scope.defaultName);
                //是否关闭全站评论功能
                $scope.siteConfigAuditType=$scope.siteConfig.auditType;
                //全局评论颜色问题
                $scope.commStyleColor=$scope.siteConfig.styleColor;
                commentStyle=$scope.commStyleColor;
                $rootScope.changeComStyle();

                //获取最新和热门评论
                $scope.getNewCom();
                $scope.getHotCom();

                //setTimeout(function () {

                //},1000)
            }).error(function(data,header,config,status){

            });

        };
        $scope.getCommentInfo();

        //点击评论数，跳转
        $scope.commentJump=function(){
            if($scope.newCommenCon.length>0){
                var scrollDistance=$("#newComCon").offset().top-60;
                $("body").animate({scrollTop:scrollDistance}, 300);
            }else if($scope.commenCon.length>0){
                var scrollDistance=$("#commenCon").offset().top-60;
                $("body").animate({scrollTop:scrollDistance}, 300);
            }else{
                return
            }
        };


        //弹出模态框
        $scope.showModeAndRepley=function ($event) {
            //if($rootScope.uid_fouNder){
                $('#commentModle').css('display',"block");
                $("#commentArea").css('display',"block");
                $("#comInput").focus();
                $("#comInput").attr("placeholder","请输入内容:");
                $("#comm_text_footer").css('display',"none");
            //}else{
            //    window.location='http://172.19.32.122:9081/SSOv2/user/login_app';
            //}
            $scope.parentId=0;
        };
        //取消模态框
        $scope.cancelCom=function () {
            //添加input图片上传的的占位
            $("#uploadImagForm").css({display:'block'});
            //清除选中图片
            $(".imgCont").children().remove();
            //清除上传图片的对象
            window.uploadFile="";
            $("#comInput").val("");
            $('#commentModle').css('display',"none");
            $("#commentArea").css('display',"none");
            $("#comm_text_footer").css('display',"block");
        }

        //回复评论
        $scope.replayCom=function (parentId,userName,ParentUserID) {
            $('#commentModle').css('display',"block");
            $("#commentArea").css('display',"block");
            $("#comm_text_footer").css('display',"none");
            // 提交评论使用
            $scope.parentId=parentId;
            $scope.userName=userName;
            $scope.ParentUserID=ParentUserID;
            $("#comInput").attr("placeholder","@"+userName+":");
        };

        // 发送评论
        $scope.sendCom=function () {
            if(!window.uploadFile){
                //只提交文字评论
                var sendMesg=$("#comInput").val();
                console.log("评论信息");
                console.log(sendMesg);
                var logUserId=uid_fouNder;
                var LogUserName=username_fouNder;
                var topicTitle=$scope.actDetailData.title;
                //匿名评论
                if(!uid_fouNder){
                    logUserId=0;
                    LogUserName="触屏用户";
                }
                var siteID = 1, rootID = articleId, parentID = $scope.parentId, parentUser = $scope.userName,
                    parentUserID = $scope.ParentUserID, topic =topicTitle, userID=logUserId
                    , userName=LogUserName, type = 0,content=sendMesg;

                //提交回复
                $.ajax({
                    type: "post",
                    url: sentComent,
                    data:{
                        "siteID": siteID,
                        "rootID": rootID,
                        "parentID": parentID,
                        "parentUser": parentUser,
                        "parentUserID": parentUserID,
                        "topic": topic,
                        "userID": userID,
                        "userName": userName,
                        "type": type,
                        "content":content,
                        "channel":1,
                        "sourceType":actSource
                    },
                    dataType : "json",
                    //jsonp: "jsoncallback",
                    //jsoncallback:"jsoncallback",
                    success: function(data){
                            console.log(data);
                            alert("评论成功！");
                            $scope.getNewCom();
                            console.log("发送内容为：");
                            console.log(sendMesg);
                            $("#comInput").val("");
                            $('#commentModle').css('display',"none");
                            $("#commentArea").css('display',"none");
                            $("#comm_text_footer").css('display',"block");

                    },
                    error: function(){
                        alert("评论失败！2");
                        $("#comInput").val("");
                        $('#commentModle').css('display',"none");
                        $("#commentArea").css('display',"none");
                        $("#comm_text_footer").css('display',"block");
                    }
                });
            }else{
                //提交评论和图片
                //即提交评论文字，又上传图片

                var imgUrl=window.uploadFile;
                console.log("提交的图片信息")
                console.log(imgUrl)

                //图片提交
                var formdata = new FormData();
                formdata.append("Filedata", imgUrl);
                formdata.append("siteID", 1);
                formdata.append("location", 0);
                formdata.append("userId", "0");
                formdata.append("fileType", "picture");
                formdata.append("uniqid", "B715C9A0-D580-46F2-ABD1-F244EA24C934");

                $.ajax({
                    type:'POST',
                    url: commUpload,
                    processData: false,
                    data: formdata,
                    async:false,
                    contentType: false,    //这个一定要写
                    dataType:'json',
                    success: function(result){
                        //成功后处理代码
                        //self.onSuccess(file, result);
                        //if(self.curUploadFile.length==0){
                        // 回调全部完成方法
                        //self.onComplete("全部完成");
                        console.log("上传成功");
                        console.log(result)
                        console.log("上传成功url");
                        var comimgUrl=JSON.parse(result).fileList[0].url;
                        console.log("comimgUrl")
                        console.log(comimgUrl);


                        setTimeout(function(){
                            //只提交文字评论
                            var sendMesg=$("#comInput").val();
                            console.log("评论信息");
                            console.log(sendMesg);
                            var logUserId=uid_fouNder;
                            var LogUserName=username_fouNder;
                            var topicTitle=$scope.actDetailData.title;
                            //匿名评论
                            if(!uid_fouNder){
                                logUserId=0;
                                LogUserName="触屏用户";
                            }
                            var siteID = 1, rootID = articleId, parentID = $scope.parentId, parentUser = $scope.userName,
                                parentUserID = $scope.ParentUserID, topic =topicTitle, userID=logUserId
                                , userName=LogUserName, type = 0,content=sendMesg;

                            //提交回复
                            $.ajax({
                                type: "post",
                                url: sentComent,
                                data:{
                                    "siteID": siteID,
                                    "rootID": rootID,
                                    "parentID": parentID,
                                    "parentUser": parentUser,
                                    "parentUserID": parentUserID,
                                    "topic": topic,
                                    "userID": userID,
                                    "userName": userName,
                                    "type": type,
                                    "content":content,
                                    "channel":1,
                                    "sourceType":actSource,
                                    "imgUrl":comimgUrl
                                },
                                dataType : "json",
                                //jsonp: "jsoncallback",
                                //jsoncallback:"jsoncallback",
                                success: function(data){
                                    console.log(data);
                                    alert("上传成功");
                                    $scope.getNewCom();
                                    console.log("发送内容为：");
                                    console.log(sendMesg);
                                    $("#comInput").val("");
                                    window.uploadFile=""
                                    //添加input图片上传的的占位
                                    $("#uploadImagForm").css({display:'block'});
                                    //清除选中图片
                                    $(".imgCont").children().remove();
                                    $('#commentModle').css('display',"none");
                                    $("#commentArea").css('display',"none");
                                    $("#comm_text_footer").css('display',"block");

                                },
                                error: function(){
                                    alert("上传失败");
                                    $("#comInput").val("");
                                    window.uploadFile="";
                                    //添加input图片上传的的占位
                                    $("#uploadImagForm").css({display:'block'});
                                    //清除选中图片
                                    $(".imgCont").children().remove();
                                    $('#commentModle').css('display',"none");
                                    $("#commentArea").css('display',"none");
                                    $("#comm_text_footer").css('display',"block");
                                }
                            });

                    },1000)
                    },
                    error: function(){
                        self.onFailure(file);
                    }

                });

            }
        };
        //最新评论子评论
        $scope.lunchaMorChilCom=true;
        //获取最新子评论加载更多更多内容 id为每条父评论的id号
        $scope.checkMorChildCom=function (id,index) {
            $scope.lunchaMorChilCom=false;

            $http.jsonp(replayComent+'id='+id+'&page=0&siteId=1&source=6&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                //点击查看更多子评论之前
                $scope.beforLoadMorChid=$scope.newCommenCon[index].topDiscuss.list;
                //点击查看更多子评论之后
                $scope.newCommenCon[index].topDiscuss.list=data.list;

                // 是否还有更多评论
                /*console.log("是否有更多")
                 console.log(data.hasMore)*/
                $scope.hasNoChildMorCom={};
                $scope.hasChildMorCom={};
                if(data.hasMore){
                    $scope.hasChildMorCom.index=true;
                }else{
                    $scope.hasNoChildMorCom.index=true;

                }
                //获取评论
                // $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        };

        $scope.hotlunchaMorChilCom=true;
        // 热门评论子评论
        //获取子评论加载更多更多内容 id为每条父评论的id号
        $scope.hotcheckMorChildCom=function (id,index) {
            $scope.hotlunchaMorChilCom=false;

            $http.jsonp(replayComent+'id='+id+'&page=0&siteId=1&source=6&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                //点击查看更多子评论之前
                $scope.hotbeforLoadMorChid=$scope.commenCon[index].topDiscuss.list;
                //点击查看更多子评论之后
                $scope.commenCon[index].topDiscuss.list=data.list;

                // 是否还有更多评论
                /*console.log("是否有更多")
                 console.log(data.hasMore)*/
                $scope.hothasNoChildMorCom={};
                $scope.hothasChildMorCom={};
                if(data.hasMore){
                    $scope.hothasChildMorCom.index=true;
                }else{
                    $scope.hothasNoChildMorCom.index=true;

                }
                //获取评论
                // $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        }


        //回到顶部
        $(".fiBtn").hide();
        $(window).on("scroll", function(){
            if($(window).scrollTop() >0){
                $(".fiBtn").show();
            }else{
                $(".fiBtn").hide();
            }
        });
        $scope.goBackTop=function () {
            $("body").animate({scrollTop: 0}, 300);
        };
    }]);
